#pragma once
#include "fixedvector.h"

template<typename T,uint32 size>
struct hashmap_t
{
	T* buf[size];

	hashmap_t()
	{
		clear();
	}

	void insert(T* elem)
	{
		uint32 k=elem->gethashkey() & (size-1);
		elem->nextinhashmap=buf[k];
		buf[k]=elem;
	}

	T* get(uint32 key)
	{
		uint32 k=key & (size-1);

		T* ptr;

		for (ptr=buf[k]; ptr && ptr->gethashkey()!=key; ptr=ptr->nextinhashmap)
		{
		}

//		ASSERT(ptr);

		return ptr;
	}

	void clear()
	{
		memset (buf,0,sizeof(buf));
	}
};


template<typename T,uint32 buffersize,uint32 hashmapsize>
struct hashcontainer_t
{
	fixedvector<T,buffersize> buf;
	hashmap_t<T,hashmapsize> map;

	T* get(const stringhash& name){return map.get(name.value);}
	T* allocate(const stringhash& name)
	{
		buf.resize(buf.size()+1);
		buf.back().name=name;
		map.insert(&buf.back());
		return &buf.back();
	}

	T* get(uint32 index)
	{
		return &buf[index];
	}

	uint32 size() const
	{
		return buf.size();
	}

	void flush()
	{
		buf.clear();
		map.clear();
	}
};